using System;
using System.Collections;
using System.Collections.Generic;
using CinemaDirector;
using UnityEngine;

public class DailySpinLayer : MonoBehaviour
{
	[Serializable]
	public class SpinPrize
	{
		public int coins;
	}

	public DSLights lights;

	public PullButton pullButton;

	public ScrollWheel scrollWheel;

	public float slowFrom = 0.1f;

	public float slowTo = 0.5f;

	public int repeatTimes = 7;

	public UISprite spinBck;

	public Color normalSpinBckCol;

	public Color highlightSpinBckCol;

	public Cutscene winAnim;

	public GGTweenCount countAnim;

	public UILabel coinsLabel;

	public GameObject topPanel;

	public GameObject confeti;

	public GameObject doneButton;

	public GameObject closeButton;

	public GameObject tomorrowLabel;

	public List<SpinPrize> prizes = new List<SpinPrize>();

	protected bool spinStarted;

	private void Awake()
	{
		UnityEngine.Random.seed = (int)DateTime.Now.Ticks;
		int startIndex = UnityEngine.Random.Range(0, prizes.Count);
		scrollWheel.Init(SetItemLookDelegate, startIndex);
	}

	private SpinPrize PrizeForIndex(int index)
	{
		index %= prizes.Count;
		if (index < 0)
		{
			index = prizes.Count + index;
		}
		return prizes[index];
	}

	private void SetItemLookDelegate(GameObject item, int index)
	{
		SpinPrize spinPrize = PrizeForIndex(index);
		UIPrizeItem component = item.GetComponent<UIPrizeItem>();
		component.SetPrize(spinPrize.coins.ToString());
	}

	private void OnDisable()
	{
		Ads.instance.hideBanner(false);
	}

	private void OnEnable()
	{
		Ads.instance.hideBanner(true);
		tomorrowLabel.SetActive(false);
		if (GGPlayerSettings.instance.MultiplayerMatchesPlayed() <= 1)
		{
			closeButton.SetActive(false);
		}
		else
		{
			closeButton.SetActive(true);
			GGPlayerSettings.instance.walletManager.CollectDailyCoins();
		}
		doneButton.SetActive(false);
		topPanel.SetActive(false);
		StopAllCoroutines();
		spinStarted = false;
		confeti.SetActive(false);
		lights.ShowLights();
		pullButton.onClick = OnStartSpin;
		pullButton.onPressed = OnPullButtonPressed;
		spinBck.color = normalSpinBckCol;
		winAnim.CutsceneFinished -= CutsceneHandler;
		winAnim.Stop();
		winAnim.CutsceneFinished += CutsceneHandler;
	}

	private void CutsceneHandler(object sender, CutsceneEventArgs e)
	{
		tomorrowLabel.SetActive(true);
		doneButton.SetActive(true);
	}

	private void OnPullButtonPressed(bool pressed)
	{
		if (!spinStarted)
		{
			lights.SetColor((!pressed) ? Color.white : Color.green);
		}
	}

	private void OnStartSpin()
	{
		if (spinStarted)
		{
			return;
		}
		spinStarted = true;
		closeButton.SetActive(false);
		spinBck.color = normalSpinBckCol;
		int num = prizes.Count - repeatTimes + UnityEngine.Random.Range(0, prizes.Count);
		lights.SetColor(Color.white);
		scrollWheel.StartSpin(slowFrom, num, delegate
		{
			scrollWheel.SlowDownSpin(slowFrom, slowTo, repeatTimes, delegate
			{
				GGPlayerSettings instance = GGPlayerSettings.instance;
				SpinPrize spinPrize = PrizeForIndex(scrollWheel.index);
				Debug.Log("Prize " + spinPrize.coins);
				countAnim.from = instance.Model.coins;
				countAnim.to = instance.Model.coins + spinPrize.coins;
				instance.walletManager.AddCurrency(CurrencyType.coins, spinPrize.coins);
				GGPlayerSettings.instance.walletManager.CollectDailyCoins();
				coinsLabel.text = countAnim.from.ToString();
				BlinkSpinBck(7, delegate
				{
					confeti.SetActive(true);
					winAnim.Play();
				});
			});
		});
	}

	private void BlinkSpinBck(int times, Action onComplete)
	{
		StopAllCoroutines();
		StartCoroutine(DoBlinkSpinBck(times, onComplete));
	}

	private IEnumerator DoBlinkSpinBck(int times, Action onComplete)
	{
		float blinkDuration = 0.1f;
		Color fromCol = normalSpinBckCol;
		Color toCol = highlightSpinBckCol;
		for (int repeatedTimes = 0; repeatedTimes < times || times < 0; repeatedTimes++)
		{
			float time = 0f;
			while (time <= blinkDuration)
			{
				time += Time.deltaTime;
				spinBck.color = Color.Lerp(fromCol, toCol, time / blinkDuration);
				yield return null;
			}
			Color tmp = toCol;
			toCol = fromCol;
			fromCol = tmp;
		}
		if (onComplete != null)
		{
			onComplete();
		}
	}

	public void OnDone()
	{
		NavigationManager.instance.Pop();
	}
}
